from PyQt5.QtWidgets import (QApplication, QMainWindow, QWidget, QVBoxLayout, QHBoxLayout,
                           QPushButton, QLabel, QLineEdit, QTextEdit, QFileDialog, QMessageBox)
from PyQt5.QtCore import Qt
from PyQt5.QtGui import QFont, QIcon, QPalette, QColor
import sys
import threading
from main import run_automation

class MainWindow(QMainWindow):
    def __init__(self):
        super().__init__()
        self.setWindowTitle("农行纪念币预约自动填写工具")
        self.setGeometry(100, 100, 800, 600)
        
        # 设置窗口样式
        self.setStyleSheet("""
            QMainWindow {
                background-color: #ffffff;
            }
            QLabel {
                font-size: 14px;
                color: #333333;
            }
            QPushButton {
                padding: 8px 20px;
                border-radius: 4px;
                font-size: 14px;
                font-weight: 500;
            }
            QLineEdit {
                padding: 8px 12px;
                border: 1px solid #e8e8e8;
                border-radius: 4px;
                font-size: 14px;
                background: #fafafa;
                min-height: 20px;
            }
            QLineEdit:focus {
                border-color: #40a9ff;
                background: #ffffff;
            }
            QTextEdit {
                border: 1px solid #e8e8e8;
                border-radius: 4px;
                padding: 8px;
                font-size: 13px;
                line-height: 1.5;
                background: #fafafa;
            }
            QTextEdit:focus {
                border-color: #40a9ff;
                background: #ffffff;
            }
        """)
        
        # 创建主窗口部件
        central_widget = QWidget()
        self.setCentralWidget(central_widget)
        layout = QVBoxLayout(central_widget)
        layout.setSpacing(20)
        layout.setContentsMargins(30, 30, 30, 30)
        
        # 标题
        title_label = QLabel("农行纪念币预约自动填写工具")
        title_label.setStyleSheet("""
            font-size: 24px;
            color: #1890ff;
            font-weight: bold;
            padding: 20px;
            background: #f0f5ff;
            border-radius: 8px;
            margin-bottom: 20px;
        """)
        title_label.setAlignment(Qt.AlignCenter)
        layout.addWidget(title_label)
        
        # 文件选择部分
        file_widget = QWidget()
        file_widget.setStyleSheet("""
            QWidget {
                background: #ffffff;
                border: 1px solid #f0f0f0;
                border-radius: 8px;
                padding: 20px;
            }
        """)
        file_layout = QHBoxLayout(file_widget)
        file_layout.setContentsMargins(15, 15, 15, 15)
        
        excel_label = QLabel("Excel文件：")
        excel_label.setStyleSheet("font-weight: 500;")
        self.file_path = QLineEdit()
        self.file_path.setPlaceholderText("请选择Excel文件...")
        self.file_path.setReadOnly(True)
        
        select_button = QPushButton("选择文件")
        select_button.setStyleSheet("""
            QPushButton {
                background-color: #1890ff;
                color: white;
                border: none;
                min-width: 100px;
            }
            QPushButton:hover {
                background-color: #40a9ff;
            }
            QPushButton:pressed {
                background-color: #096dd9;
            }
        """)
        
        file_layout.addWidget(excel_label)
        file_layout.addWidget(self.file_path)
        file_layout.addWidget(select_button)
        layout.addWidget(file_widget)
        
        # 状态显示部分
        status_widget = QWidget()
        status_widget.setStyleSheet("""
            QWidget {
                background: #ffffff;
                border: 1px solid #f0f0f0;
                border-radius: 8px;
                padding: 20px;
            }
        """)
        status_layout = QVBoxLayout(status_widget)
        
        status_label = QLabel("处理状态")
        status_label.setStyleSheet("""
            font-weight: 500;
            font-size: 16px;
            color: #1890ff;
            padding-bottom: 10px;
        """)
        status_layout.addWidget(status_label)
        
        self.status_area = QTextEdit()
        self.status_area.setReadOnly(True)
        self.status_area.setMinimumHeight(200)
        status_layout.addWidget(self.status_area)
        
        layout.addWidget(status_widget)
        
        # 开始按钮
        self.start_button = QPushButton("开始处理")
        self.start_button.setFixedSize(160, 45)
        self.start_button.setStyleSheet("""
            QPushButton {
                background-color: #1890ff;
                color: white;
                border: none;
                font-size: 16px;
                font-weight: bold;
            }
            QPushButton:hover {
                background-color: #40a9ff;
            }
            QPushButton:pressed {
                background-color: #096dd9;
            }
            QPushButton:disabled {
                background-color: #bfbfbf;
            }
        """)
        layout.addWidget(self.start_button, alignment=Qt.AlignCenter)
        
        # 连接信号
        select_button.clicked.connect(self.browse_file)
        self.start_button.clicked.connect(self.start_process)
        
    def browse_file(self):
        filename, _ = QFileDialog.getOpenFileName(
            self,
            "选择Excel文件",
            "",
            "Excel Files (*.xlsx *.xls)"
        )
        if filename:
            self.file_path.setText(filename)
            
    def update_status(self, message):
        self.status_area.append(message)
        scrollbar = self.status_area.verticalScrollBar()
        scrollbar.setValue(scrollbar.maximum())
        
    def start_process(self):
        if not self.file_path.text():
            QMessageBox.warning(self, "错误", "请先选择Excel文件！")
            return
            
        self.start_button.setEnabled(False)
        self.status_area.clear()
        self.update_status("开始处理...")
        
        def run():
            try:
                run_automation(self.file_path.text(), self.update_status)
                self.update_status("处理完成！")
            except Exception as e:
                self.update_status(f"发生错误: {str(e)}")
            finally:
                self.start_button.setEnabled(True)
                
        threading.Thread(target=run, daemon=True).start()

if __name__ == "__main__":
    app = QApplication(sys.argv)
    app.setStyle('Fusion')
    window = MainWindow()
    window.show()
    sys.exit(app.exec_()) 